import { reactive, ref } from "vue";
import type { SelectProps } from "ant-design-vue";
import { getDataApi } from "@/api/configCenter/templateLibrary";

export default function InitHook() {
  //表单数据
  const formState = reactive<Partial<BusinessWaybill.FormStateType>>({
    id: undefined,
    exportFileName: undefined,
    fieldNames: undefined
  });

  const templateOptions = ref<SelectProps["options"]>([]);
  const getDataValue = ref<Array<BusinessWaybill.AllValueType>>([]);

  //等待状态
  const loading = ref<boolean>(false);

  const getData = () => {
    loading.value = true;
    return getDataApi({ pageNum: 0, pageSize: 999, total: 0, beforePageNum: 0, name: "", status: 1, dataScope: 0 })
      .then((res: any) => {
        const { code, data } = res;
        const { records } = data;
        if (code === 200) {
          getDataValue.value = records;
          templateOptions.value = records.map((t: any) => {
            return { label: t.name ? t.name : "-", value: t.id };
          });
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };

  //表单ref
  const formRef = ref(null);
  return {
    formState,
    loading,
    formRef,
    templateOptions,
    getData,
    getDataValue
  };
}
